package com.gayworld.controller.web.places;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.gayworld.common.CommonJson;
import com.gayworld.controller.vo.RateVO;
import com.gayworld.service.model.RateService;

@Controller
@RequestMapping(value = "/rate")
public class RateController {

	private final Logger logger = LogManager.getLogger(this.getClass()
			.getName());

	@Autowired
	private RateService rateService;

	@RequestMapping(value = "/add.do", method = RequestMethod.GET)
	public @ResponseBody
	CommonJson addRate(@RequestParam(required = true) Integer id,
			@RequestParam(required = true) String comment,
			@RequestParam(required = true) Float rate,
			HttpServletRequest request) {
		logger.debug("START");
		String userIpAddress = request.getRemoteAddr();
		logger.debug("iList " + userIpAddress);
		CommonJson cj = new CommonJson();
		cj.setMessage(String.valueOf(rateService.addComment(id, comment, rate,
				userIpAddress)));
		return cj;

	}

	@RequestMapping(value = "/getComments.do", method = RequestMethod.GET)
	public @ResponseBody
	List<RateVO> getRates(@RequestParam(required = true) Integer id) {
		logger.debug("START");
		List<RateVO> rateVOlist = rateService.getRatesByPlaceId(id);
		logger.debug("rateVOlist " + rateVOlist.size());
		return rateVOlist;
	}

}
